//
//  HomeStressCell.m
//  Ginix
//
//  Created by fzq on 2025/2/20.
//

#import "HomeStressCell.h"
#import <ZZCircleProgress/ZZCircleProgress.h>

@interface HomeStressCell()
@property (nonatomic,strong) UIImageView * infoView;
@property (nonatomic,strong) YYLabel * relaxLabel;
@property (nonatomic,strong) YYLabel * normalabel;
@property (nonatomic,strong) YYLabel * mediumLabel;
@property (nonatomic,strong) YYLabel * highLabel;

@property (nonatomic,strong) ZZCircleProgress * progressView;
@property (nonatomic,strong) ZZCircleProgress * noProgressView;
@property (nonatomic,strong) ZZCircleProgress * meProgressView;
@property (nonatomic,strong) ZZCircleProgress * hiProgressView;

@end

@implementation HomeStressCell

-(void)setSubViewLayout{
    
    [super setSubViewLayout];
    
    self.titleLabel.text = @"心理压力状态";

    
    [self addSubview:self.infoView];
    [_infoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_offset(CGSizeMake(120 * FMScaleX, 120 * FMScaleX));
        make.right.mas_offset(-25 * FMScaleX);
        make.top.equalTo(self.iconView.mas_bottom).offset(17 * FMScaleX);
    }];
    
    
    [self.infoView addSubview:self.progressView];
    [_progressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(@0);
        make.size.mas_offset(CGSizeMake(90 * FMScaleX, 90 * FMScaleX));
    }];
    [self.infoView addSubview:self.noProgressView];
    [_noProgressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(@0);

        make.size.mas_offset(CGSizeMake(70 * FMScaleX, 70 * FMScaleX));
    }];
    [self.infoView addSubview:self.meProgressView];
    [_meProgressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(@0);
        make.size.mas_offset(CGSizeMake(50 * FMScaleX, 50 * FMScaleX));
    }];
    [self.infoView addSubview:self.hiProgressView];
    [_hiProgressView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(@0);
        make.size.mas_offset(CGSizeMake(30 * FMScaleX, 30 * FMScaleX));
    }];
    
    
    [self addSubview:self.relaxLabel];
    [_relaxLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgview.mas_left).offset(30 * FMScaleX);
        make.top.equalTo(self.iconView.mas_bottom).offset(18 * FMScaleX);
        make.right.equalTo(self.infoView.mas_left).offset(-6 * FMScaleX);
        make.height.mas_offset(22 * FMScaleX);

    }];
    [self addSubview:self.normalabel];
    [_normalabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.relaxLabel.mas_bottom).offset(10 * FMScaleX);
        make.left.right.height.equalTo(self.relaxLabel);
    }];
    
    [self addSubview:self.mediumLabel];
    [_mediumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.normalabel.mas_bottom).offset(10 * FMScaleX);
        make.left.right.height.equalTo(self.relaxLabel);

    }];
    [self addSubview:self.highLabel];
    [_highLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mediumLabel.mas_bottom).offset(10 * FMScaleX);
        make.left.right.height.equalTo(self.relaxLabel);
    }];
    
}

-(void)setInfoModel:(HomeStressModel *)infoModel{
    
    self.relaxLabel.attributedText = modifyStringWithColoredDotAndNumberSize([NSString stringWithFormat:@"放松: %@ %@",infoModel.relax.ratio,infoModel.relax.duration], Bold(12), Bold(20));
        self.normalabel.attributedText = modifyStringWithColoredDotAndNumberSize([NSString stringWithFormat:@"正常: %@ %@",infoModel.normal.ratio,infoModel.normal.duration],Bold(12), Bold(20));
    self.mediumLabel.attributedText = modifyStringWithColoredDotAndNumberSize( [NSString stringWithFormat:@"中等: %@ %@",infoModel.medium.ratio,infoModel.medium.duration],Bold(12), Bold(20));
    self.highLabel.attributedText = modifyStringWithColoredDotAndNumberSize([NSString stringWithFormat:@"偏高: %@ %@",infoModel.high.ratio,infoModel.high.duration],Bold(12), Bold(20));
    
    if (stringContainsNumber(infoModel.relax.ratio)) {
        self.progressView.progress = [[infoModel.relax.ratio stringByReplacingOccurrencesOfString:@"%" withString:@""] doubleValue] /100;
    }
    if (stringContainsNumber(infoModel.normal.ratio)) {
        self.noProgressView.progress = [[infoModel.normal.ratio stringByReplacingOccurrencesOfString:@"%" withString:@""] doubleValue] /100;
    }
    if (stringContainsNumber(infoModel.medium.ratio)) {
        self.meProgressView.progress = [[infoModel.medium.ratio stringByReplacingOccurrencesOfString:@"%" withString:@""] doubleValue] /100;
    }
    if (stringContainsNumber(infoModel.high.ratio)) {
        self.hiProgressView.progress = [[infoModel.high.ratio stringByReplacingOccurrencesOfString:@"%" withString:@""] doubleValue] /100;
    }    
}

BOOL stringContainsNumber(NSString *string) {
    NSScanner *scanner = [NSScanner scannerWithString:string];
    NSInteger number;
    return [scanner scanInteger:&number];
}


NSMutableAttributedString *modifyStringWithColoredDotAndNumberSize(NSString *originalString, UIFont *defaultFont, UIFont *numberFont) {
    // 创建富文本字符串
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] init];
    
    UIColor *dotColor;
    // 根据内容改变圆点颜色
    if ([originalString containsString:@"放松"] ) {
        dotColor = RDRGB(0x799BE8);
    } else if([originalString containsString:@"正常"]){
        dotColor = RDRGB(0x6FBBA4);
    }else if ([originalString containsString:@"中等"]){
        dotColor = RDRGB(0xF3B66E );
    }else{
        dotColor = RDRGB(0xF295A5);
    }
    
    // 添加彩色圆点
    NSAttributedString *dot = [[NSAttributedString alloc] initWithString:@"●" attributes:@{NSForegroundColorAttributeName: dotColor}];
    [attributedString appendAttributedString:dot];
    
    // 添加空格
    [attributedString appendAttributedString:[[NSAttributedString alloc] initWithString:@" "]];
    
    // 遍历原始字符串
    for (NSUInteger i = 0; i < originalString.length; i++) {
        unichar character = [originalString characterAtIndex:i];
        if (isdigit(character)) {
            // 如果是数字，设置数字字体
            NSAttributedString *numberChar = [[NSAttributedString alloc] initWithString:[NSString stringWithCharacters:&character length:1] attributes:@{NSFontAttributeName: numberFont}];
            [attributedString appendAttributedString:numberChar];
        } else {
            // 如果不是数字，设置默认字体
            NSAttributedString *nonNumberChar = [[NSAttributedString alloc] initWithString:[NSString stringWithCharacters:&character length:1] attributes:@{NSFontAttributeName: defaultFont}];
            [attributedString appendAttributedString:nonNumberChar];
        }
    }
    
    return attributedString;
}


-(UIImageView *)infoView{
    
    if(!_infoView){
        _infoView = [[UIImageView alloc]init];
        _infoView.image = [UIImage imageNamed:@"Home_list_stress"];
    }
    return _infoView;
}

-(YYLabel *)relaxLabel{
    
    if (!_relaxLabel) {
        _relaxLabel = [[YYLabel alloc]init];
    }
    return _relaxLabel ;
}

-(YYLabel *)normalabel{
    
    if (!_normalabel) {
        _normalabel = [[YYLabel alloc]init];
    }
    return _normalabel ;
}

-(YYLabel *)mediumLabel{
    
    if (!_mediumLabel) {
        _mediumLabel = [[YYLabel alloc]init];
    }
    return _mediumLabel ;
}

-(YYLabel *)highLabel{
    
    if (!_highLabel) {
        _highLabel = [[YYLabel alloc]init];
    }
    return _highLabel ;
}

-(ZZCircleProgress *)progressView{
    
    if(!_progressView){
        _progressView = [[ZZCircleProgress alloc]initWithFrame:CGRectZero pathBackColor:RDRGB(0xF3EEE7) pathFillColor:RDRGB(0x799BE8) startAngle:-90 strokeWidth:4];
        _progressView.progress = 0.59;
        _progressView.showPoint = NO;
        _progressView.showProgressText = NO;
    }
    return _progressView;
    
}
-(ZZCircleProgress *)noProgressView{
    
    if(!_noProgressView){
        _noProgressView = [[ZZCircleProgress alloc]initWithFrame:CGRectZero pathBackColor:RDRGB(0xF3EEE7) pathFillColor:RDRGB(0x6FBBA4) startAngle:-90 strokeWidth:4];
        _noProgressView.progress = 0.22;
        _noProgressView.showPoint = NO;
        _noProgressView.showProgressText = NO;
    }
    return _noProgressView;
    
}
-(ZZCircleProgress *)meProgressView{
    
    if(!_meProgressView){
        _meProgressView = [[ZZCircleProgress alloc]initWithFrame:CGRectZero pathBackColor:RDRGB(0xF3EEE7) pathFillColor:RDRGB(0xF295A5) startAngle:-90 strokeWidth:4];
        _meProgressView.progress = 0.1;
        _meProgressView.showPoint = NO;
        _meProgressView.showProgressText = NO;
    }
    return _meProgressView;
    
}
-(ZZCircleProgress *)hiProgressView{
    
    if(!_hiProgressView){
        _hiProgressView = [[ZZCircleProgress alloc]initWithFrame:CGRectZero pathBackColor:RDRGB(0xF3EEE7) pathFillColor:RDRGB(0xF3B66E) startAngle:-90 strokeWidth:4];
        _hiProgressView.progress = 0.08;
        _hiProgressView.showPoint = NO;
        _hiProgressView.showProgressText = NO;
    }
    return _hiProgressView;
    
}
@end
